import { ajaxAsync } from 'vue-xiaobu-utils'
import StationInfoModel from './model/StationInfoModel'
import LineInfoModel from './model/LineInfoModel'
import { getColletctedRouteList } from './CollectionUtils'

// window.NEARBY_SITE_LIST = [3, 3, 3, 3]
// window.ROUTE_HAS_DIRECTION = false

const busInfoPostUrl = isRouteHasDirection()
  ? '/bus/homeStationWithDirection'
  : '/bus/homeStation'

// 判断是否走有方向的附近站点协议
function isRouteHasDirection() {
  if (window.ROUTE_HAS_DIRECTION === false) {
    return false
  } else {
    return true
  }
}

// 获取附近站点需要的数量
function getNearbySiteList() {
  return window.NEARBY_SITE_LIST ? window.NEARBY_SITE_LIST : [3, 3, 3, 3, 3]
}

/**
 * 获取附近站点信息，带换向的
 * @param {Number} longitude
 * @param {Number} latitude
 */
async function getBusInfoWithDirection(longitude = 0, latitude = 0) {
  let postData = {
    LONGITUDE: longitude + '',
    LATITUDE: latitude + '',
    COUNTS: getNearbySiteList()
  }
  let collectedRouteList = await getColletctedRouteList()
  let collectedRouteIdList = getCollectedRouteIdList(collectedRouteList)
  if (collectedRouteIdList > 0) {
    // 如果有收藏的路线，需要将该路线id列表作为参数发送协议
    postData.SUBROUTE_IDS = collectedRouteIdList
  }
  let res = await ajaxAsync(busInfoPostUrl, postData)
  let stationList = []
  if (res.LIST) {
    res.LIST.map(item => {
      let stationId = item.STATION_ID
      let busList = []
      if (item.BUSES) {
        item.BUSES.map(busItem => {
          // 将上下行数据放在一个列表中
          let busModelList = []
          if (isRouteHasDirection()) {
            // 请求有来回方向的协议的话就解析上下行数据
            if (busItem.BUSES_UP) {
              let busUpModel = parseBusLineModel(stationId, busItem.BUSES_UP)
              busModelList.push(busUpModel)
            }
            if (busItem.BUSES_DOWN) {
              let busDownModel = parseBusLineModel(
                stationId,
                busItem.BUSES_DOWN
              )
              busModelList.push(busDownModel)
            }
          } else {
            // 否则返回的直接是一个线路信息
            let busModel = parseBusLineModel(stationId, busItem)
            busModelList.push(busModel)
          }
          if (busModelList.length > 0) {
            // 解析线路收藏状态
            parseRouteCollected(busModelList, collectedRouteList)
            busList.push(busModelList)
          }
        })
      }
      // 转换站点数据
      let stationModel = new StationInfoModel(
        stationId,
        item.STATIONNAME,
        item.DISTANCE,
        item.LONGITUDE,
        item.LATITUDE,
        busList
      )
      stationList.push(stationModel)
    })
  }
  return stationList
  // return []
}

/**
 * 将所有的收藏的线路的id整理成一个集合
 * @param {*} collectedRouteList
 */
function getCollectedRouteIdList(collectedRouteList) {
  let idList = []
  if (collectedRouteList) {
    collectedRouteList.map(routeInfo => {
      if (idList.indexOf(routeInfo.SUBROUTE_ID) === -1) {
        idList.push(routeInfo.SUBROUTE_ID)
      }
    })
  }
  console.log('~~~idList~~~', idList)
  if (idList.length > 0) {
    return idList
  } else {
    return null
  }
}

/**
 * 转换线路公交信息
 * @param {String} stationId 最近站点id
 * @param {Object} busItem 线路公交数据
 */
function parseBusLineModel(stationId, busItem) {
  let price = (parseFloat(busItem.PRICE) / 100).toFixed(2)
  return new LineInfoModel(
    busItem.SUBROUTE_ID,
    busItem.SUBROUTE_NAME,
    busItem.TYPE,
    busItem.BUS_START_STATION,
    busItem.BUS_END_STATION,
    busItem.NOWBUS_STOP,
    stationId,
    busItem.STARTTIME,
    busItem.ENDTIME,
    price
  )
}

/**
 * 处理线路是否收藏
 * @param {Array} busModelList 转换后的线路公交数据列表
 * @param {Array} collectedRouteList 收藏的线路信息列表
 */
function parseRouteCollected(busModelList, collectedRouteList) {
  if (!collectedRouteList || busModelList.length === 0) return
  let isCollected = false
  for (let i = 0; i < collectedRouteList.length; i++) {
    let routeInfo = collectedRouteList[i]
    busModelList.map(busModel => {
      if (
        // eslint-disable-next-line
        routeInfo.SUBROUTE_ID == busModel.routeId &&
        // eslint-disable-next-line
        routeInfo.TYPE == busModel.direction
      ) {
        isCollected = true
        busModel.setCollected(isCollected)
      }
    })
    // if (isCollected) {
    //   break
    // }
  }
}

export { getBusInfoWithDirection }
